﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SharpPcap.WinPcap;
using BasePlatformCommon.devChain;

namespace BasePlatformNet
{
	public class NetDevice
	{
        public string BroadcastMAC = "ffffffffffff";

		public int index = 0;								// 设备索引

		public string name = "";							// 网口名称

		public string mac = "";								// mac地址

		public string ip = "";								// ip地址

        public string mask = "255.255.255.0";               // 子网掩码

        public string BroadcastIP {
            get {
                if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(mask)) {
                    return "255.255.255.255";
                }
                var IPPart = ip.Split('.');
                var MaskPart = mask.Split('.');
                var iptext = String.Format("{0}.{1}.{2}.{3}", MaskPart[0].Equals("0") ? "255" : IPPart[0], MaskPart[1].Equals("0") ? "255" : IPPart[1], MaskPart[2].Equals("0") ? "255" : IPPart[2], MaskPart[3].Equals("0") ? "255" : IPPart[3]);
                return iptext;
            }
        }                       // 广播地址

		public bool activeStatus = false;					// 网口活动状态：连接有设备, 即为活动网口

		public WinPcapDevice pcapDevice = null;				// SharpPcap设备

		public PacketBuilder packetBuilder = null;			// 组包器
		public PacketSender packetSender = null;			// 发送器
		public PacketReceiver packetReceiver = null;		// 接收器
		
		public List<ControlNode> subCtrlList_0 = null;		// 分控列表
		public List<ControlNode> equipmentList_0 = null;	// 附属设备列表

		public List<ControlNode> subCtrlList_1 = null;		// 分控列表
		public List<ControlNode> equipmentList_1 = null;	// 附属设备列表

		public Hashtable ctrlNodeChain_0 = null;			// 拓扑结构链
		public Hashtable ctrlNodeChain_1 = null;			// 拓扑结构链

		public int totalCtrlNodeNum_0 = -1;					// 设备总数目
		public int totalCtrlNodeNum_1 = -1;					// 设备总数目

		public int currentOperFlag = 0;						// 操作标记

		public ReplyInfo[] readSubReplyList = null;			// 读操作反馈表 - 分控
		public ReplyInfo[] readEquReplyList = null;			// 读操作反馈表 - 附属设备

		private Random myRandom = null;						// 随机数生成器

		public FramePacketPara framePacketPara = null;				// 参数对象 - 帧包
		public DispConfigPacketPara dispConfigPacketPara = null;	// 参数对象 - 显示设置包

		public int lastDetectSubNum_0 = 0;
		public int lastDetectSubNum_1 = 0;
		public int lastDetectEquNum_0 = 0;
		public int lastDetectEquNum_1 = 0;
		public int lostPacketTimes_0 = 0;
		public int lostPacketTimes_1 = 0;

		// 构造函数
		public NetDevice(string paraMac)
		{
			mac = paraMac;

			myRandom = new Random();
			packetBuilder = new PacketBuilder(mac);

			packetSender = new PacketSender(packetBuilder);
			packetReceiver = new PacketReceiver();

			subCtrlList_0 = new List<ControlNode>();
			subCtrlList_1 = new List<ControlNode>();

			equipmentList_0 = new List<ControlNode>();
			equipmentList_1 = new List<ControlNode>();

			ctrlNodeChain_0 = new Hashtable();
			ctrlNodeChain_1 = new Hashtable();

			readSubReplyList = new ReplyInfo[7000];
			for (int k = 0; k < 7000; k++)
			{
				readSubReplyList[k] = new ReplyInfo();
			}

			readEquReplyList = new ReplyInfo[7000];
			for (int k = 0; k < 7000; k++)
			{
				readEquReplyList[k] = new ReplyInfo();
			}
		}

		// 清除该网口下检测到的设备
		public void clearSubEquList()
		{
			if (currentOperFlag == 0)
			{
				subCtrlList_0.Clear();
				equipmentList_0.Clear();
			}
			else
			{
				subCtrlList_1.Clear();
				equipmentList_1.Clear();
			}
		}

		// 设置发包参数对象
		public void setParaObjs(FramePacketPara framePacketPara, DispConfigPacketPara dispConfigPacketPara)
		{
			// 处理 "一行字节数" 和 "行包字节数" 的关系
			if (framePacketPara.row_bytes < LEDPacket.SHOW_DATA_LENGTH)
			{
				framePacketPara.packetDataSize = framePacketPara.row_bytes;
			}
			else
			{
				framePacketPara.packetDataSize = LEDPacket.SHOW_DATA_LENGTH;
			}

			this.framePacketPara = framePacketPara;
			this.dispConfigPacketPara = dispConfigPacketPara;

			// 设置发送器的发包参数对象
			packetSender.setParaObjs(framePacketPara, dispConfigPacketPara);
		}

		// ToString()
		public override string ToString()
		{
			return name + " | " + activeStatus;
		}

        public string getIP() 
        {
            return ip;
        }
	}
}
